/**
 *
 */
package com.yidu.springbootdemo.permission.action;

import com.yidu.hbats.service.emp.AtsEmpAthleteService;
import com.yidu.hbats.service.emp.AtsEmpCoachService;
import com.yidu.hbats.service.emp.AtsEmpExpertService;
import com.yidu.hbats.service.emp.AtsEmpLeaderService;
import com.yidu.springbootdemo.permission.action.vo.TcharacterVo;
import com.yidu.springbootdemo.permission.action.vo.TroleUserVo;
import com.yidu.springbootdemo.permission.action.vo.TuserVo;
import com.yidu.springbootdemo.permission.domain.Tuser;
import com.yidu.springbootdemo.permission.service.TcharacterService;
import com.yidu.springbootdemo.permission.service.TroleUserService;
import com.yidu.springbootdemo.permission.service.TuserService;
import com.yidu.springbootdemo.permission.tools.Messages;
import com.yidu.springbootdemo.permission.tools.Pages;
import com.yidu.springbootdemo.permission.tools.Tools;
import com.yidu.utils.FileTools;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * 类的描述：用户控制器
 *
 * @author HJJ
 * @date 2019年11月30日 上午8:41:38
 */
@Controller
@RequestMapping("/user")
public class UserController {
    @Resource
    TcharacterService tcharacterService;

    @Resource
    TuserService tuserService;

    @Resource
    TroleUserService troleUserService;

    @Resource
    FileTools fileTools;

    /**
     * 中心领导信息接口
     */
    @Resource
    private AtsEmpLeaderService atsEmpLeaderService;

    /**
     * 教练员信息接口
     */
    @Resource
    private AtsEmpCoachService atsEmpCoachService;

    /**
     * 专家信息接口
     */
    @Resource
    private AtsEmpExpertService atsEmpExpertService;

    /**
     * 运动员信息接口
     */
    @Resource
    private AtsEmpAthleteService atsEmpAthleteService;

    /**
     * 查所有角色
     *
     * @return
     */
    @RequestMapping("/selectAll")
    @ResponseBody
    public List<TcharacterVo> findAlljs() {
        return tcharacterService.findAll2();
    }

    /**
     * 增加或修改
     *
     * @return
     * @throws IOException
     */
    @RequestMapping("/addOrup")
    @ResponseBody
    public Messages addOrup(TuserVo tch, HttpServletRequest request) throws IOException {
        Messages msg = new Messages();
        //下拉框真实姓名的值
        String trueUserText = request.getParameter("trueUserText");
        //职业的值
        String zhiyeText = request.getParameter("zhiyeText");
        String zhiyeVal = request.getParameter("zhiyeValue");
        String userId1 = request.getParameter("userId");

        tch.setZhiyeText(zhiyeText);
        tch.setZhiyeVal(zhiyeVal);
        tch.setUserId1(userId1);
        if (!StringUtils.isEmpty(userId1)) {
            tuserService.delete(userId1);
        }

        // 增加用户
        String path = "";
        if (tch.getUserImgae() != null && !"".equals(tch.getUserImgae())) {

            path = fileTools.saveFile("user", tch.getUserImgae());
        }
        String rd = UUID.randomUUID().toString().replaceAll("-", "");
        tch.setUserId(rd);
        tch.setUserPhoto(path);
        tch.setUserState("Y");
        tch.setUserSort(null);
        String time = Tools.time();
        tch.setUserFoundDate(time);
        //得到登录的用户
        Tuser user = (Tuser) request.getSession().getAttribute("user");
        if (user == null) {
            tch.setUserFoundUser("hjj");
        } else {
            tch.setUserFoundUser(user.getUserName());
        }
        if(tch.getTrueUser() == null){
            tch.setFilId(tch.getTrueUserNum());
        }else{
            tch.setFilId(tch.getTrueUser());
        }
        tch.setCompanyName(trueUserText);
        tch.setCompanyNum(zhiyeVal);
        tch.setCompany(zhiyeText);
        Tuser us = new Tuser();
        BeanUtils.copyProperties(tch, us);
        tuserService.add(us);

        msg.setMsg("成功");
        msg.setStatus(1);
        return msg;
    }

    /**
     * 显示所有
     *
     * @return
     */
    @RequestMapping("/findAll")
    @ResponseBody
    public Map<String, Object> findAll(TuserVo tch, HttpServletRequest request) {
        Map<String, Object> map = new HashMap<String, Object>();
        int page = Integer.valueOf(request.getParameter("page"));
        int limit = Integer.valueOf(request.getParameter("limit"));
        // 将 得到的页数 以及limit 放入到pages 的工具类中
        Pages pages1 = new Pages();
        pages1.setCurPage(page);
        pages1.setLimit(limit);
        int totalRows = tuserService.count(tch);
        pages1.setTotalRows(totalRows);
        List<TuserVo> list = tuserService.findAll(tch, pages1);// 查数据
        map.put("code", 0);
        map.put("msg", "");
        map.put("count", totalRows);
        map.put("data", list);
        return map;
    }

    /**
     * 删除
     *
     * @return
     */
    @RequestMapping("/delete")
    @ResponseBody
    public Messages delete(String modID) {
        Messages msg = new Messages();

        tuserService.delete(modID);
        msg.setMsg("成功");
        msg.setStatus(1);
        return msg;
    }

    /**
     * 	修改出现内容
     * @return
     */
    @RequestMapping("/selectOne")
    @ResponseBody
    public Map<String, Object> selectOne(String userId) {
        Map<String, Object> map = new HashMap<String, Object>();
        List<TroleUserVo> us = troleUserService.findAll(userId);

        TuserVo vo = tuserService.findByID(userId);
        map.put("list", us);
        map.put("vo", vo);
        return map;
    }
    /**
     * 查询是否已经登录
     * @param session
     * @return
     */
    @RequestMapping("/findModByManager")
    @ResponseBody
    public Tuser findModByManager(HttpSession session) {

        Tuser man = (Tuser) session.getAttribute("user");
        if(man==null){
            man = new Tuser();
            man.setUserId("0");
            man.setUserName("请重新登录");
        }
        return man;
    }
}
